/**
 * @Author: LiMeng
 * @Date: 2017/4/26
 */
var GLOBAL = {
    ID: $.getQueryParam("id"),
    DATA_GRID_ROW_INDEX: {
        dg1: 0
    },
    YEARS: []
};
var getSelectedUser = function (data) {
    var names = '';
    var ids = '';
    for (var index in data) {
        names += ',' + data[index].realName;
        ids += ',' + data[index].id;
    }

    $('#teacherName').textbox('setValue', names.substring(1));
    $('#teacherId').val(ids.substring(1));
};
var openSelectUser = function (title, callback) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['900px', '500px'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml' + '?isMulti=1',
        callBack: callback
    });
};
var getRecentTwentyYearsComboList = function () {
    var thisYear = new Date().getFullYear();
    for (var i = thisYear + 10; i > thisYear - 10; i--) {
        GLOBAL.YEARS.push({
            value: i,
            text: i
        });
    }
};
var toggleComboBox = function () {
    var $isTrainOutside = $('#isTrainOutside');
    var $trainDeptId = $('#trainDeptId');
    var $trainDeptNameOutside = $('#trainDeptNameOutside');
    var $wrap_trainDeptId = $trainDeptId.parents('.form_one');
    var $wrap_trainDeptNameOutside = $trainDeptNameOutside.parents('.form_one');

    $isTrainOutside.combobox({
        onChange: function () {
            if ($(this).val().toString() === '0') {
                $wrap_trainDeptId.removeClass('hide');
                $wrap_trainDeptNameOutside.addClass('hide');
                $trainDeptId.textbox('enable');
                $trainDeptNameOutside.textbox('disable');
            } else {
                $wrap_trainDeptId.addClass('hide');
                $wrap_trainDeptNameOutside.removeClass('hide');
                $trainDeptId.textbox('disable');
                $trainDeptNameOutside.textbox('enable');
            }
        }
    });
};
var attachAddRowEvent = function (dataGridId) {
    $('#' + dataGridId + '_btn_add').on('click', function () {
        layer.alert('为防止数据丢失，请先保存！');
        return false;
    });
};
$(function () {
    getRecentTwentyYearsComboList();

    $('#trainTypeId').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.CONTRACTOR_TRAIN_TYPE,
        valueField: 'id',
        textField: 'text',
        required: true,
        editable: false
    });

    $('#year').combobox({
        data: GLOBAL.YEARS,
        value: new Date().getFullYear(),
        editable: false,
        required: true
    });

    $('#teacherName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择授课人', getSelectedUser);
            }
        }],
        editable: false
    });

    $('#trainDeptId').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        required: true,
        editable: false
    });

    toggleComboBox();

    $('#dg1').datagrid({
        method: 'post',
        url: ROOT_PATH + '/contractor/businessQualification/list.shtml',
        queryParams: {
            contractorId: $.getQueryParam('id'),
            sort: 'addTime',
            order: 'desc'
        },
        idField: 'id',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        sortOrder: true,
        striped: true,
        fit: false,
        height: 300,
        singleSelect: true,
        columns: [[
            {field: 'ckid', checkbox: true},
            {
                field: 'action', title: '操作', width: 80, align: 'center', formatter: function (value, row, index) {
                var ele_edit = '<a id="dg1_btn_edit_' + row.id + '" href="javascript:void(0)" class="easyui-linkbutton text-primary" plain="true">编辑</a> ';
                var ele_save = '<a id="dg1_btn_save_' + row.id + '" href="javascript:void(0)" class="easyui-linkbutton text-primary hide" plain="true">保存</a> ';
                var ele_delete = '<a id="dg1_btn_delete_' + row.id + '" href="javascript:void(0)" class="easyui-linkbutton text-primary" plain="true">删除</a>';
                var ele_cancel = '<a id="dg1_btn_cancel_' + row.id + '" href="javascript:void(0)" class="easyui-linkbutton text-primary hide" plain="true">取消</a>';
                return ele_edit + ele_save + ele_delete + ele_cancel;
            }
            },
            {
                field: 'personName',
                title: '姓名',
                width: 80,
                sortable: true,
                editor: {
                    type: 'textbox', options: {required: true}
                }
            },
            {
                field: 'idcard',
                title: '身份证号',
                width: 80,
                editor: {type: 'textbox', options: {required: true}}
            },
            {
                field: 'contractorName',
                title: '承包商名称',
                width: 100,
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            },
            {
                field: 'score',
                title: '成绩',
                width: 50,
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            },
            {
                field: 'isQualified',
                title: '是否合格',
                width: 80,
                sortable: true,
                editor: {
                    type: 'combobox', options: {
                        required: true,
                        data: [{
                            value: 1,
                            text: '是'
                        }, {
                            value: 0,
                            text: '否'
                        }]
                    }
                }
            },
            {
                field: 'workTerm',
                title: '作业有效期限',
                width: 100,
                sortable: true,
                editor: {type: 'datebox'}
            },
            {
                field: 'workType',
                title: '工种',
                width: 80,
                sortable: true,
                editor: {type: 'textbox'}
            },
            {field: 'addTime', title: '登记日期', width: 100, sortable: true},
            {field: 'contractorId', title: '承包商id', hidden: true}
        ]]
    });

    $('#btn_save').on('click', function () {
        $.messager.progress();	// 显示进度条

        $('#form1').form('submit', {
            url: ROOT_PATH + '/contractor/staffTrain/add.shtml',
            onSubmit: function (param) {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.progress('close');	// 如果表单是无效的则隐藏进度条
                }
                return isValid;	// 返回false终止表单提交
            },
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success) {
                    $.messager.progress('close');	// 如果提交成功则隐藏进度条
                    layer.msg(result.message, {time: 1000, icon: 1}, function () {
                        $.getIframeWindow().$('#dg1').datagrid('load');
                        var id = result.data.id;
                        window.location.href = ROOT_PATH + '/contractor/staffTrain/edit.shtml?'
                            + 'id=' + id
                            + '&resourceId=' + $.getQueryParam('resourceId')
                            + '&state=edit';
                    });
                } else {
                    $.messager.progress('close');	// 如果提交成功则隐藏进度条
                    layer.msg(result.message, {icon: 2});
                }
            }
        });
    });

    attachAddRowEvent('dg1');
});